package com.spzx.payment.service.impl;

import com.alibaba.fastjson.JSON;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import com.alipay.api.response.AlipayTradeWapPayResponse;
import com.spzx.common.core.exception.ServiceException;
import com.spzx.payment.configure.AlipayConfig;
import com.spzx.payment.domain.PaymentInfo;
import com.spzx.payment.service.IAlipayService;
import com.spzx.payment.service.IPaymentInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;

@Service
public class AlipayServiceImpl implements IAlipayService {

    @Autowired
    AlipayClient alipayClient;

    @Autowired
    IPaymentInfoService paymentInfoService;


    /**
     <form name="punchout_form" method="post"
     action="https://openapi-sandbox.dl.alipaydev.com/gateway.do?charset=utf-8&method=alipay.trade.wap.pay&sign=gVobnuw2RfPbV%2B1pMvLL%2BSFGblPYRwG5lrVPHPNfaiba99Vm3vMFppPiF7NcxA7TNFS8zGe47GP8pJ%2BErKDczFJ%2F6VcxWLH9B9rgOEJXx1%2BQ1pNyXxo6b4dIR1WNhfVluSFK8mMJfiY%2B%2BGCloAESHLy%2BJz0EW9tHRfKbwIAWq%2BExbs6rsq0HmlAnwdufVlaHCgfBObbDNEhP6Un9x7g4B42ukwjfg6ZB0WoIiaze6dkAjP6vXqlrHuWQAIEfGbhgwamEHXCJVlwf0Pvifo2vIFEyj8WolD2PiarifMKV9UjbjKvWvrTjl9cuDOeHreeoWT76rx2SMSxkJNvO4tzS%2BQ%3D%3D&return_url=http%3A%2F%2Fry-spzx.atguigu.cn%2F%23%2Fpages%2Fmoney%2FpaySuccess&notify_url=http%3A%2F%2Fspzx0923.free.idcfengye.com%2Fpayment%2Falipay%2Fcallback%2Fnotify&version=1.0&app_id=9021000123611999&sign_type=RSA2&timestamp=2025-01-18+15%3A29%3A25&alipay_sdk=alipay-sdk-java-4.8.73.ALL&format=json">
     <input type="hidden" name="biz_content" value="{&quot;out_trade_no&quot;:&quot;b4f2aead7d9e45548da8b0746c55a3b4&quot;,&quot;total_amount&quot;:19.00,&quot;subject&quot;:&quot;X1000 白色 + 4G;X1000 红色 + 8G;&quot;,&quot;product_code&quot;:&quot;QUICK_WAP_WAY&quot;}">
     <input type="submit" value="立即支付" style="display:none" >
     </form>
     <script>document.forms[0].submit();</script>
     */
    @Override
    public String submitAlipay(String orderNo) {
        //1.保存支付信息并返回支付信息
        PaymentInfo paymentInfo = paymentInfoService.savePaymentInfo(orderNo);

        //2.调用支付宝生成h5表单页面
        AlipayTradeWapPayRequest request = new AlipayTradeWapPayRequest(); //商家和支付宝签约：手机网页版支付方式。

        try {
            request.setReturnUrl(AlipayConfig.return_payment_url); //同步回调地址    支付宝扣款后执行这个地址调用，通知用户扣款成功。(不作为最终支付依据。)
            request.setNotifyUrl(AlipayConfig.notify_payment_url); //异步回调地址    支付宝扣款后执行这个地址调用，通知商品平台后端扣款成功。后端需要完成业务扩展(修改支付信息，修改订单信息，减库存等)

            // 必选参数
            // 声明一个map 集合
            HashMap<String, Object> map = new HashMap<>();
            map.put("out_trade_no",paymentInfo.getOrderNo()); //商品平台生成唯一订单号
            map.put("product_code","QUICK_WAP_WAY"); //手机网站支付类型    签约
            //*************************************************************************
            map.put("total_amount",paymentInfo.getAmount()); // 此次课程 用的沙箱版 支付宝支付    模拟真正的支付宝支付    假的。
            //map.put("total_amount","0.01");
            //*************************************************************************
            map.put("subject",paymentInfo.getContent());

            request.setBizContent(JSON.toJSONString(map)); //请求携带数据

            AlipayTradeWapPayResponse response = alipayClient.pageExecute(request);//默认  method="POST"
            String h5Form = response.getBody(); //调用支付接口，生成支付表单页面，简称："h5"
            System.out.println("h5Form = " + h5Form);
            return h5Form;
        } catch (AlipayApiException e) {
            throw new ServiceException("调用支付接口失败");
        } catch (Exception e) {
            throw new ServiceException(e.getMessage());
        }
    }
}
